package com.gensdai.leliang.common.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.gensdai.leliang.R;
import com.gensdai.leliang.common.bean.TitleBean;

import butterknife.BindView;
import butterknife.ButterKnife;

/**首页通用标题的适配  比如 ： 热卖推荐 more
 * Created by LT on 2017/6/15.
 */

public class ContentTitleAdapterRec extends DelegateAdapter.Adapter<ContentTitleAdapterRec.ContentTitleViewHolder> {

    private Context mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    int count = 1;

    TitleBean bean;

    OnMoreClickListener click;

    public void setOnMoreClickListener(OnMoreClickListener click){
        this.click = click;
    }

    public ContentTitleAdapterRec(Context context, LayoutHelper layoutHelper, TitleBean bean) {
        this(context, layoutHelper, bean, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public ContentTitleAdapterRec(Context context, LayoutHelper layoutHelper, TitleBean bean, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.bean = bean;
        this.mLayoutParams = layoutParams;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public ContentTitleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ContentTitleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_main_content_title, parent, false));
    }

    @Override
    public void onBindViewHolder(ContentTitleViewHolder holder, int position) {
        holder.itemView.setLayoutParams(
                new VirtualLayoutManager.LayoutParams(mLayoutParams));
        if (bean != null){
            holder.moreLable.setVisibility(bean.isShowMore()?View.VISIBLE:View.GONE);
            holder.titleLable.setText(bean.getTitle());
            holder.titleLable.setTextColor(bean.getColor());
            holder.moreLable.setTextColor(bean.getColor());
//            Uri uri = new Uri.Builder().scheme("res").path(String.valueOf(bean.getImageResid())).build();
//            holder.icon.setImageURI(uri);

            holder.moreLable.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(click != null){
                        click.onMoreClickListener(bean.getType());
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return count;
    }


    static class ContentTitleViewHolder extends RecyclerView.ViewHolder {

//        @BindView(R.id.icon)
//        SimpleDraweeView icon;
        @BindView(R.id.titleLable)
        TextView titleLable;
        @BindView(R.id.moreLable)
        TextView moreLable;

        public ContentTitleViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }

    public interface OnMoreClickListener{
        void onMoreClickListener(int type);
    }

}
